<?php

class ProblemsController extends AppController {
	var $scaffold = 'admin';
	/*
	 * get($id) - returns the content of a specific question
	 *
	 * data:
	 *
	 * [GET] id : id of question
	 *
	 * return:
	 *
	 * title : title of question.
	 * content : content of question.
	 * options[] : json object of options.
	 * success: 1 if success. otherwise 0.
	 * errmsg(optional): error message when fail.
	 * errcode(optional): error code.
	 *
	 */
	var $uses=array("Problem","User","Scoreboard");
	public function index(){
		try{
			$result=$this->Problem->find("all",array("conditions"=>array("open <>"=>0),"fields"=>array("id","title")));
			foreach($result as $key=>$val){
				$problems[]=array("id"=>$val["Problem"]["id"],"title"=>$val["Problem"]["title"]);
			}
			$this->set('problem',array("success"=>1,"problems"=>$problems));
		}catch(Exception $exception){
			$this->set('problem',array("success"=>0,"errmsg"=>$exception->getMessage()));
		}
		$this->set('json',array("problem"));
	}
	public function get($id=null){
		try{
			if($id==null){
				$result=$this->Problem->find("all",array("conditions"=>array("open <>"=>0)));//,array('conditions' => array('Problem.id' => $id)));
				foreach($result as $key=>$val){
					$problems[$val["Problem"]["id"]]=array("id"=>$val["Problem"]["id"],"title"=>$val["Problem"]["title"],"content"=>$val["Problem"]["content"],"options"=>array($val["Problem"]["opt1"],$val["Problem"]["opt2"],$val["Problem"]["opt3"],$val["Problem"]["opt4"],$val["Problem"]["opt5"]));
				}
			}else{
				$result=$this->Problem->find("first",array("conditions"=>array("open <>"=>0,"id"=>$id)));
				if($result===false)throw new Exception("沒有這個問題唷揪咪");
				$problems[$result["Problem"]["id"]]=array("id"=>$result["Problem"]["id"],"title"=>$result["Problem"]["title"],"content"=>$result["Problem"]["content"],"options"=>array($result["Problem"]["opt1"],$result["Problem"]["opt2"],$result["Problem"]["opt3"],$result["Problem"]["opt4"],$result["Problem"]["opt5"]));
			}
			$this->set('problem',array("success"=>1,"problems"=>$problems));
		}catch(Exception $exception){
			$this->set('problem',array("success"=>0,"errmsg"=>$exception->getMessage()));
		}
		$this->set('json',array("problem"));
	}

	/*
	 * answer($id,$answer) - answering an question
	 *
	 * data:
	 *
	 * [GET] id : id of question
	 * [GET] answer : answer of question
	 *
	 * return:
	 *
	 * success: 1 if success. otherwise 0.
	 * errmsg(optional): error message when fail.
	 * errcode(optional): error code.
	 *
	 */

	public function answer($id,$answer){
		try{
			App::import('Helper', 'Time');
			$time = new TimeHelper();
			$penalty=microtime(true)-$time->toUnix($problem["Problem"]["open_time"]);
			$problem=$this->Problem->findById($id);
			$user=$this->Auth->user();
			$data=array(
			"Scoreboard"=>array(
				"user_id"=>$user["User"]["id"],
				"problem_id"=>$id,
				"answer"=>$answer,
				"ip"=>$this->_get_real_ip(),
				"penalty"=>$panelty
			));
			if($problem["Problem"]["open"]==0){
				throw new Exception('未開放唷揪咪');
			}
			if($problem["Problem"]["open"]==2){
				throw new Exception('過期了唷揪咪');
			}
			if($this->Scoreboard->find('count',array("conditions"=>array("Scoreboard.user_id"=>$user["User"]["id"],"Scoreboard.problem_id"=>$id)))>0){
				throw new Exception("答過囉");
			}
			$this->correct=0;
			if($problem["Problem"]["ans"]!=$answer){
				$data["Scoreboard"]["point"]=0;
				$this->correct=0;
			}else{
				$data["Scoreboard"]["point"]=5;
				$this->correct=1;
			}
			if(!$this->Scoreboard->save($data)){
				throw new Exception("Save failed.");
			}
			$this->set('problem',array("success"=>1,"correct"=>$this->correct));
			$this->set('json',array("problem"));
		}catch(Exception $exception){
			$this->set('problem',array("success"=>0,"errmsg"=>$exception->getMessage()));
			$this->set('json',array("problem"));
		}
	}

	public function admin_open($id){
		try{
			App::import('Helper', 'Time'); // loadHelper('Html'); in CakePHP 1.1.x.x
			$time = new TimeHelper();
			$problem=$this->Problem->findById($id);
			$data=array("Problem"=>array("id"=>$id,"open"=>1,"open_time"=>date('Y-m-d H:i:s')));
			if(!$this->Problem->save($data)){
				throw new Exception("Save failed.");
			}
			$this->set('problem',array("success"=>1));
			$this->set('json',array("problem"));
			$this->redirect($this->referer());
		}catch(Exception $exception){
			$this->set('problem',array("success"=>0,"errmsg"=>$exception->getMessage()));
			$this->set('json',array("problem"));
			$this->redirect($this->referer());
		}
	}
	public function admin_close($id){
		try{
			App::import('Helper', 'Time'); // loadHelper('Html'); in CakePHP 1.1.x.x
			$time = new TimeHelper();
			$problem=$this->Problem->findById($id);
			$data=array("Problem"=>array("id"=>$id,"open"=>2,"close_time"=>date('Y-m-d H:i:s')));
			if(!$this->Problem->save($data)){
				throw new Exception("Save failed.");
			}
			$this->set('problem',array("success"=>1));
			$this->set('json',array("problem"));
			$this->redirect($this->referer());
		}catch(Exception $exception){
			$this->set('problem',array("success"=>0,"errmsg"=>$exception->getMessage()));
			$this->set('json',array("problem"));
			$this->redirect($this->referer());
		}
	}
}
?>
